package com.system.insurance.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.common.result.Result;
import com.system.insurance.entity.*;
import com.system.insurance.service.AppletUserService;
import com.system.insurance.service.ContentSettingService;
import com.system.insurance.service.FormFillingService;
import com.system.insurance.service.SmallInsuranceTypeService;
import com.system.insurance.utils.AuthContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.bouncycastle.asn1.cms.PasswordRecipientInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 用户保险内容填写 前端控制器
 * </p>
 *
 * @author WS
 * @since 2024-05-15
 */

@Api(tags = "询价用户保险内容")
@RestController
@RequestMapping("/form-filling")
public class FormFillingController {

    @Autowired
    private FormFillingService formFillingService;

    @Autowired
    private ContentSettingService contentSettingService;

    @Autowired
    private SmallInsuranceTypeService smallInsuranceTypeService;

    @Autowired
    private AppletUserService appletUserService;

    @ApiOperation(value = "后台：查看所有用户保险内容")
    @GetMapping("/formFillingList")
    public Result<List<FormFilling>> formFillingList(){
        List<FormFilling> list = formFillingService.list();
        return Result.ok(list);
    }


    @ApiOperation(value = "报价方：查看报价方保险内容")
    @GetMapping("/formFillingNr")
    public Result<ContentSetting> formFillingById(@RequestBody FormFilling formFilling){

        SmallInsuranceType byId = smallInsuranceTypeService.getById(formFilling.getSmallid());

        ContentSetting contentSetting = contentSettingService.getById(byId.getQuotationId());

        return Result.ok(contentSetting);
    }


    @ApiOperation(value = "询价方：查看自己所有询价订单")
    @GetMapping("/formFillingXj")
    public Result<List<FormFilling>> formFillingxj(){

        String s = AuthContextUtil.get();
        LambdaQueryWrapper<FormFilling> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(FormFilling::getXjUserId,Integer.valueOf(s));
        List<FormFilling> list = formFillingService.list(wrapper);

        return Result.ok(list);
    }


//    @ApiOperation(value = "询价方：查看自己所有报价单")
//    @GetMapping("/formFillingBjd")
//    public Result<List<FormFilling>> formFillingBjd(){
//
//        String s = AuthContextUtil.get();
//        AppletUser byId = appletUserService.getById(Integer.valueOf(s));
//
//        LambdaQueryWrapper<FormFilling> wrapper = new LambdaQueryWrapper<>();
//
//        wrapper.eq(FormFilling::getXjUserId,Integer.valueOf(s));
//        List<FormFilling> list = formFillingService.list(wrapper);
//
//        for (int i = 0; i < list.size(); i++) {
//            if (byId.getNum()>list.size()){
//
//                FormFilling formFilling = list.get(i);
//                formFilling.setNum(1);
//                formFillingService.save(formFilling);
//            }else {
//                FormFilling formFilling = list.get(i);
//                formFilling.setNum(2);
//                formFillingService.save(formFilling);
//            }
//
//        }
//        return Result.ok(list);
//    }


//    @ApiOperation(value = "询价方：查看自己单个报价单")
//    @GetMapping("/formFillingBjdById/{id}")
//    public Result<FormFilling> formFillingBjdById(@PathVariable Integer id){
//
//        String s = AuthContextUtil.get();
//        LambdaQueryWrapper<FormFilling> wrapper = new LambdaQueryWrapper<>();
//
//        wrapper.eq(FormFilling::getXjUserId,Integer.valueOf(s)).eq(FormFilling::getId,id);
//        FormFilling list = formFillingService.getOne(wrapper);
//
//        return Result.ok(list);
//    }


    @ApiOperation(value = "报价方：查看报价方填写完的保险内容")
    @GetMapping("/formFillingTx/{smallId}")
    public Result<ContentSetting> formFilling(@PathVariable Integer smallId){


        LambdaQueryWrapper<SmallInsuranceType> wrapper = new LambdaQueryWrapper<>();

//        FormFilling one = formFillingService.getById(id);

        SmallInsuranceType byId = smallInsuranceTypeService.getById(smallId);

        wrapper.eq(SmallInsuranceType::getDirectorysId,byId.getDirectorysId());
        SmallInsuranceType one = smallInsuranceTypeService.getOne(wrapper);
        ContentSetting list = contentSettingService.getById(one.getDirectorysId());

        return Result.ok(list);
    }


    @ApiOperation(value = "报价方客户询价列表：查看单个小保险内所有用户询价保险内容")
    @GetMapping("/formFillingListBySmallId/{smallId}")
    public Result<List<FormFilling>> formFillingListBySmallId(@PathVariable Integer smallId){

        LambdaQueryWrapper<FormFilling> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FormFilling::getSmallid,smallId);
        List<FormFilling> list = formFillingService.list(wrapper);
        return Result.ok(list);
    }

    @ApiOperation(value = "添加询价用户保险内容")
    @PostMapping("/saveFormFilling/{smallId}")
    public Result<FormFilling> saveFormFilling(@PathVariable Integer smallId,@RequestBody FormFilling formFilling){

        String s = AuthContextUtil.get();
        FormFilling filling = new FormFilling();
        filling.setCreateTime(LocalDateTime.now());
        filling.setContent(formFilling.getContent());
        filling.setXjUserId(Integer.valueOf(s));
        filling.setSmallid(smallId);
        filling.setXjname(formFilling.getXjname());
        filling.setCompany(formFilling.getCompany());
        filling.setIntentionalPrice(formFilling.getIntentionalPrice());

        LambdaQueryWrapper<SmallInsuranceType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SmallInsuranceType::getId,smallId);
        SmallInsuranceType one = smallInsuranceTypeService.getOne(wrapper);

        one.setNumber(one.getNumber()+1);
        smallInsuranceTypeService.updateById(one);

        boolean save = formFillingService.save(filling);

        if (save){
            return Result.ok(formFilling).message("添加成功");
        }
        return Result.fail(formFilling).message("添加失败");
    }

//    @ApiOperation(value = "报价方: 添加报价方保险内容")
//    @PostMapping("/saveFormFillingbao/{smallId}/{appletUserId}")
//    public Result<FormFilling> saveFormFillingbao(@PathVariable Integer smallId,@PathVariable Integer appletUserId,@RequestBody FormFilling formFilling){
//        formFilling.setCreateTime(LocalDateTime.now());
//
//        String s = AuthContextUtil.get();
//        formFilling.setXjUserId(appletUserId);
//        formFilling.setBjUserId(Integer.valueOf(s));
//        formFilling.setSmallid(smallId);
//
//        formFilling.setStart(2);
//        boolean save = formFillingService.save(formFilling);
//
//        LambdaQueryWrapper<FormFilling> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(FormFilling::getSmallid,smallId).eq(FormFilling::getXjUserId,appletUserId).eq(FormFilling::getStart,1);
//        FormFilling one = formFillingService.getOne(wrapper);
//        one.setNumber(one.getNumber()+1);
//
//        formFillingService.updateById(one);
//        if (save){
//            return Result.ok(formFilling).message("添加成功");
//        }
//        return Result.fail(formFilling).message("添加失败");
//    }


    @ApiOperation(value = "报价方: 添加报价方保险内容")
    @PostMapping("/saveFormFillingbao/{id}")
    public Result<FormFilling> saveFormFillingbao(@PathVariable Integer id,@RequestBody FormFilling formFilling){
        formFilling.setCreateTime(LocalDateTime.now());

        String s = AuthContextUtil.get();
        FormFilling byId = formFillingService.getById(id);

        boolean save = formFillingService.save(formFilling);

        LambdaQueryWrapper<FormFilling> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FormFilling::getSmallid,formFilling.getSmallid());
        FormFilling one = formFillingService.getOne(wrapper);
        one.setNumber(one.getNumber()+1);

        formFillingService.updateById(one);
        if (save){
            return Result.ok(formFilling).message("添加成功");
        }
        return Result.fail(formFilling).message("添加失败");
    }

    @ApiOperation(value = "修改用户保险内容")
    @PostMapping("/updateFormFilling")
    public Result<FormFilling> updateFormFilling(@RequestBody FormFilling bigInsuranceType){
        bigInsuranceType.setUpdateTime(LocalDateTime.now());
        boolean update = formFillingService.updateById(bigInsuranceType);

        if (update){
            return Result.ok(bigInsuranceType).message("修改成功");
        }
        return Result.fail(bigInsuranceType).message("修改失败");
    }

    @ApiOperation(value = "删除用户保险内容")
    @PostMapping("/deleteFormFilling")
    public Result deleteFormFilling(@RequestBody FormFilling bigInsuranceType){

        boolean remove = formFillingService.removeById(bigInsuranceType.getId());

        if (remove){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<FormFilling>> index22(@PathVariable Long page, @PathVariable Long limit, FormFilling subject){

        Page<FormFilling> recommendPage = new Page<>(page,limit);

        LambdaQueryWrapper<FormFilling> wrapper = new LambdaQueryWrapper<>();

        Integer id = subject.getId();

        if (id!=null){
            wrapper.eq(FormFilling::getId,id);
        }

        IPage<FormFilling> iPage = formFillingService.page(recommendPage,wrapper);

        return Result.ok(iPage);
    }

}

